import urllib2

EMPTY = ""
BLANK = " "
LEFTANGLEQUOTE = "<"
RIGHTANGLEQUOTE = ">"

class Cursor:
    """current cursor information"""
    currentPosition = 0
    InTag = False
    ExpectingEndTag = False
    currentTag = ""

class HTMLNode:
    TagName = ""
    AttributeList = {}
    
    ChildNode = []
    ParentNode = None
    
class TEXTNode:
    content = ""
    ParentNode = None

class RawHTML:
    rawDocument = EMPTY
    currentPosition = 0
    expectingEndTag = False
    currentTag = EMPTY
        
    def __init__(self, url):
        if url != EMPTY:
            try:
                p = urllib2.urlopen(url)
                self.rawDocument = p.read()
                
                #initial field
                self.currentPosition = 1
                self.expectingEndTag = False
                self.currentTag = EMPTY
                
            except:
                print "URL Error"
    
    def IsFinished(self, offset = 0):
        return len(self.rawDocument) <= (self.currentPosition + offset)    
    
    def ResetCurrentPosition(self):
        self.currentPosition = 0;
    
    def Forward(self, p = 1):
        if 0 < p :
            if self.IsFinished(p):
                return self.rawDocument[self.currentPosition : len(self.rawDocument)]
            val = self.rawDocument[self.currentPosition : self.currentPosition + p]
            self.currentPosition += p
            return val
        else:
            return "" 
            
    def ForwardToChar(self, c):
        tmpPosition = self.currentPosition
        tmp = self.Forward(1)
        while tmp != c:
            tmp = self.Forward(1)
        return self.rawDocument[tmpPosition : self.currentPosition]
    
    def ForwardToString(self, s):
        tmpPosition = self.currentPosition
        newTmpPos = self.rawDocument.find(s, self.currentPosition)
        if -1 == newTmpPos:
            return ""
        self.currentPosition = newTmpPos
        return self.rawDocument[tmpPosition : newTmpPos]
        
    
    def ForwardToNextBlank(self):
        return self.ForwardToChar(BLANK)
    
    def ForwardToNextLAQ(self):
        return self.ForwardToChar(LEFTANGLEQUOTE)[:-1]
    
    def ForwardToNextRAQ(self):
        return self.ForwardToChar(RIGHTANGLEQUOTE)
    
    def GetNextTag(self):
        startPosition = self.currentPosition;
        self.ForwardToNextRAQ()
        return self.rawDocument[startPosition : self.currentPosition -1]
   
    def MoveToNext(self):
        startPosition = self.currentPosition
        self.ForwardToNextLAQ()
        return self.rawDocument[startPosition : self.currentPosition - 1]
    



rh = RawHTML("http://www.google.com");
#print rh.rawDocument;
#print rh.Forward(4)
#rh.ResetCurrentPosition()
#print rh.Forward(1)
#print rh.ForwardToNextBlank()
#print rh.ForwardToNextLAQ()
#t = rh.ForwardToNextRAQ()
#print t[:-1]
#print rh.ForwardToNextLAQ()
#rh.ResetCurrentPosition()
#print rh.ForwardToString("<html")
#print rh.FordToNextRAQ()
#print rh.ForwardToNextLAQ()
while not rh.IsFinished():
    print rh.MoveToNext()
    print rh.GetNextTag()
    

#print rh.Forward(100000)
